﻿#if !WpfApp1
using Autodesk.AutoCAD.ApplicationServices;
using Acap = Autodesk.AutoCAD.ApplicationServices.Application;
#endif
using JoinBox.JoinBox_Code.WPF.多文档标签.ViewModel;
using System.Windows.Forms;
using Application = System.Windows.Forms.Application;

using Win32API;
using static Win32API.WinApi;

namespace JoinBox
{
    public static class DocSizeTool
    {
#if true33
        public static void TiaoZheng()
        {
            var doca = Acap.DocumentManager.MdiActiveDocument;
            if (doca == null)
            {
                return;
            }

            //cad文档管理器窗口大小
            var handle_Mdi = GetParent(doca.Window.Handle);
            GetWindowRect(handle_Mdi, out RECT rect_Mdi);

            foreach (Document doc in Acap.DocumentManager)
            {
                var handle_doc = doc.Window.Handle;

                var ws = WinApi.GetWindowPlacement(handle_doc);
                if (ws.showCmd == NCmdShow.SW_SHOWNORMAL)//处于窗口正常态
                {
                    //如果被文档栏遮盖,就挤小这个文档,并平移它到下面去,相当于挤开它
                    //获取文档窗口位置信息
                    GetWindowRect(handle_doc, out RECT rect_doc);

#if fale //这种做法不可取,因为这个函数不断运行的时候会将文档栏不断往下推
                    //文档的边界是 管理器+WPF的高度 (+就是屏幕向下)
                    var Y = rect_Mdi.Top + DocumentTab.HeightDPI;
                    if (rect_doc.Top < Y)//如果在文档栏上面(遮蔽了)才管
                    {
                        rect_doc.Top = Y;
                    }
                    Y = rect_doc.Top - rect_Mdi.Top; 
#else
                    var Y = 0;
#endif 
                    //世界坐标: 文档-管理器
                    var X = rect_doc.Left - rect_Mdi.Left;
                    //-2是cad对于窗口的预留
                    WinApi.MoveWindow(handle_doc, X - 2, Y, rect_doc.Width, rect_Mdi.Height - DocumentTab.HeightDPI, true);
                }
            }
        } 
#endif

        /// <summary>
        /// 调整文档正常态时候的大小
        /// </summary>
        /// <param name="isMax">最大化时候计算贴边</param>
        public static void TiaoZheng(bool isMax = false)
        {
            var doca = Acap.DocumentManager.MdiActiveDocument;
            if (doca == null)
            {
                return;
            }
            //有一个文档是正常态,其他都会是
            var ws = WinApi.GetWindowPlacement(doca.Window.Handle);
            if (ws.showCmd != NCmdShow.SW_SHOWNORMAL)//处于窗口正常态
            {
                return;
            }

            //cad窗口大小
            GetWindowRect(Acap.MainWindow.Handle, out RECT rect_acad);
            //cad文档管理器窗口大小
            var handle_Mdi = GetParent(doca.Window.Handle);
            GetWindowRect(handle_Mdi, out RECT rect_Mdi);

            //获取桌面大小 https://www.cnblogs.com/chlyzone/archive/2012/11/05/2754601.html
            //桌面高度(无任务栏) -  cad高 - cad到桌面顶
            int ab = 0;
            if (isMax)//最大化的时候加
            {
                ab = SystemInformation.WorkingArea.Height - rect_acad.Height;
            }
            var H = rect_Mdi.Height - DocumentTab.HeightDPI + ab;

            // 最大化时候文档底边贴边,做不到!Mdi大小的控制很奇怪
            // 文档高不变
            // H = rect_doc.Bottom - rect_doc.Top;

            // 会令刷新有奇怪的效果
            // WinApi.MoveWindow(handle_Mdi, rect_Mdi, true);

            Application.DoEvents();//winform界面处理消息队列,否则会令界面卡黑色边

            foreach (Document doc in Acap.DocumentManager)
            {
                var handle_doc = doc.Window.Handle;

                //如果被文档栏遮盖,就挤小这个文档,并平移它到下面去,相当于挤开它
                //获取文档窗口位置信息
                GetWindowRect(handle_doc, out RECT rect_doc);

                //文档-管理器,cad每条边都预留了2,所以改成0,窗口就会一直溜走
                var X = rect_doc.Left - rect_Mdi.Left;
                if (X < 0)
                {
                    X = 2;
                }

                //文档超过右边就贴边
                var W = rect_doc.Right - rect_Mdi.Right;
                if (W > 0)
                {
                    W = rect_doc.Width - W;
                }
                else
                {
                    W = rect_doc.Width;
                }
                  
                //Y=0,文档永远贴上沿
                WinApi.MoveWindow(handle_doc, X - 2, 0, W, H, true);
            }

        }
    }
}
